
package com.jf.cloud.service.sysmanager.remote;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.taglib.Paging;
import com.jf.cloud.service.sysmanager.common.taglib.Wm;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.dao.ICardOptRecordService;
import com.jf.cloud.service.sysmanager.dao.ICardUserService;
import com.jf.cloud.service.sysmanager.entity.CardOptRecord;
import com.jf.cloud.service.sysmanager.entity.CardUser;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;
import com.jf.cloud.service.sysmanager.util.CodeUtil;
import com.jf.cloud.service.sysmanager.vo.CardOptUserVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工控制器
 *
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/cardOptRecord")
public class CardOptRecordController extends BaseController {

    /**
     * 日志
     */
    private static Log log = LogFactory.getLog(CardOptRecordController.class);

    @Autowired
    private TokenHandler tokenHandler;
    @Autowired
    private ICardOptRecordService cardOptRecordService;


    /**
     * 获取用户列表数据
     */
    @ResponseBody
    @RequestMapping("/data")
    public AjaxPageData<CardOptUserVO> getUserPageList(OPaging<CardOptRecord> page, String token, String platformType, HttpServletRequest request) {
        AjaxPageData<CardOptUserVO> dataPage = new AjaxPageData();
        try {
            SysStaff sysStaff = tokenHandler.getSysStaffInfoByToken(token);
            if (sysStaff == null) {
                return dataPage;
            }
            page.setOrderBy(" ORDER BY CREATE_TIME DESC ");
            page.getWm().put("STAFF_ID", sysStaff.getRowId());
            page.getWm().put("OPT_RESULT", 0);
            Paging<CardOptRecord> cardOptRecordPaging = cardOptRecordService.findPage(page, platformType);
            List<CardOptRecord> list = cardOptRecordPaging.getDataList();
            if (list != null && list.size() > 0) {
                List<Long> userList = new ArrayList<>(list.size());
                for (CardOptRecord record : list) {
                    userList.add(record.getUserId());
                }
                List<CardOptUserVO> resultList = cardOptRecordService.getCardOptUser(userList);
                Paging<CardOptUserVO> paging = new Paging<>(cardOptRecordPaging.getTotalRows(), cardOptRecordPaging.getCurPage(), cardOptRecordPaging.getPageSize());
                paging.setDataList(resultList);
                dataPage = getAjaxPageData(paging);
            } else {
                Paging<CardOptUserVO> paging = new Paging<>(cardOptRecordPaging.getTotalRows(), cardOptRecordPaging.getCurPage(), cardOptRecordPaging.getPageSize());
                dataPage = getAjaxPageData(paging);
            }
        } catch (Exception e) {
            log.error("获取用户列表数据出错！", e);
        }
        return dataPage;
    }


    /**
     * 客服提交数据
     */
    @ResponseBody
    @RequestMapping("/submit")
    public Result submit(String token, Long rowId, Integer resultCode, @RequestParam(required = false) String remark,Long failId,Long platformType,
                         @RequestParam(required = false) String ticket, @RequestParam(required = false) String recharge, HttpServletRequest request) {
        AjaxPageData<CardOptUserVO> dataPage = new AjaxPageData();
        try {
            SysStaff sysStaff = tokenHandler.getSysStaffInfoByToken(token);
            if (sysStaff == null) {
                return new Result.ResultBuiler().failed("登录失效");
            }
            if (resultCode != 1 && resultCode != 2) {
                return new Result.ResultBuiler().failed("参数有误");
            }
            boolean result = cardOptRecordService.optResult(rowId, resultCode, remark, ticket, recharge,failId,platformType,sysStaff.getRowId());
            if (result) {
                return new Result.ResultBuiler().success();
            } else {
                return new Result.ResultBuiler().failed("操作失败");
            }
        } catch (Exception e) {
            log.error("获取用户列表数据出错！", e);
            return new Result.ResultBuiler().error();
        }
    }


    /**
     * 获取用户列表数据
     */
    @ResponseBody
    @RequestMapping("/history/data")
    public AjaxPageData<CardOptUserVO> historyData(OPaging<CardOptRecord> page, String token,String userName, String platformType,String projectName, HttpServletRequest request) {
        AjaxPageData<CardOptUserVO> dataPage = new AjaxPageData();
        try {
            SysStaff sysStaff = tokenHandler.getSysStaffInfoByToken(token);
            if (sysStaff == null) {
                return dataPage;
            }
            Paging<CardOptUserVO> cardOptRecordPaging = cardOptRecordService.findPage(page, userName,platformType,projectName);
            return getAjaxPageData(cardOptRecordPaging);
        } catch (Exception e) {
            log.error("获取用户列表数据出错！", e);
        }
        return dataPage;
    }

}
